package org.interfaceadaptation.parameter.expression.converter.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.interfaceadaptation.parameter.expression.converter.Converter;
import org.interfaceadaptation.parameter.expression.converter.Converters;
import org.junit.Test;

public class ConverterForJvmTypes {

	private static Object[] integerNumberToStringTypes = { Byte.valueOf("10"),
			Short.valueOf("10"), Integer.valueOf("10"), Long.valueOf("10") };

	private static Object[] decimalNumberToStringTypes = {
			Double.valueOf("10.25"), Float.valueOf("10.25") };

	private Converters converters = Converters.getInstance();

	@Test
	public void fromStringToIntegerNumberTypes() throws Exception {
		Converter converter;
		for (Object value : integerNumberToStringTypes) {
			converter = converters.getConverter(String.class, value.getClass());
			if (converter == null) {
				fail(" could not find a converter for "
						+ String.class.getName() + " to "
						+ value.getClass().getName());
			}
			assertEquals(value, converter.from("10"));
		}

	}

	@Test
	public void fromIntegerNumberToStringTypes() throws Exception {
		Converter converter;
		for (Object value : integerNumberToStringTypes) {
			converter = converters.getConverter(value.getClass(), String.class);
			if (converter == null) {
				fail(" could not find a converter for "
						+ value.getClass().getName() + " to "
						+ String.class.getName());
			}
			assertEquals("10", converter.from(value));
		}

	}

	@Test
	public void fromStringToDecimalNumberTypes() throws Exception {
		Converter converter;
		for (Object value : decimalNumberToStringTypes) {
			converter = converters.getConverter(String.class, value.getClass());
			if (converter == null) {
				fail(" could not find a converter for "
						+ String.class.getName() + " to "
						+ value.getClass().getName());
			}
			assertEquals(value, converter.from("10.25"));
		}

	}

	@Test
	public void fromDecimalNumberToStringTypes() throws Exception {
		Converter converter;
		for (Object value : decimalNumberToStringTypes) {
			converter = converters.getConverter(value.getClass(), String.class);
			if (converter == null) {
				fail(" could not find a converter for "
						+ value.getClass().getName() + " to "
						+ String.class.getName());
			}
			assertEquals("10.25", converter.from(value));
		}

	}
	
}
